/*
 * OptionsDialog2.java
 *
 * Created on 1. červen 2009, 22:51
 */
package com.panomedic.gui;

import com.panomedic.Constants;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.ButtonModel;

/**
 *
 * @author  Yare
 */
public class OptionsDialog2 extends javax.swing.JDialog {

    private Preferences prefs;

    /** Creates new form OptionsDialog2 */
    public OptionsDialog2(java.awt.Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        prefs = Preferences.userRoot().node(Constants.defNodePath);
        initComponents();

        int met = prefs.getInt(Constants.METHOD_CHOICE, Constants.METHOD_LUT);
        if (met == Constants.METHOD_LUT) {
            buttonGroup1.setSelected(jRadioButton1.getModel(), true);
        } else if (met == Constants.METHOD_STRETCHING) {
            buttonGroup1.setSelected(jRadioButton2.getModel(), true);
        }

        ButtonModel bm = buttonGroup1.getSelection();
        if (bm == jRadioButton1.getModel()) {
            jRadioButton3.setEnabled(true);
            jRadioButton4.setEnabled(true);
            jRadioButton5.setEnabled(true);
            monoton.setEnabled(true);
        } else if (bm == jRadioButton2.getModel()) {
            jRadioButton3.setEnabled(false);
            jRadioButton4.setEnabled(false);
            jRadioButton5.setEnabled(false);
            monoton.setEnabled(false);
        }

        int smooth = prefs.getInt(Constants.SMOOTHING, 0);
        switch (smooth) {
            case (0):
                jRadioButton3.setSelected(true);
                break;
            case (1):
                jRadioButton4.setSelected(true);
                break;
            case (2):
                jRadioButton5.setSelected(true);
                break;
        }
        monoton.setSelected(prefs.getBoolean(Constants.MONOTON, false));

        blurring.setSelected(prefs.getInt(Constants.BLUR_RADIUS, 10) > 0 ? true : false);
        saveEnd.setSelected(prefs.getInt(Constants.SAVE_END, 0) > 0 ? true : false);
        saveStep.setSelected(prefs.getInt(Constants.SAVE_END, 0) > 1 ? true : false);
        if (saveEnd.isSelected()) {
            saveStep.setEnabled(true);
        } else {
            saveStep.setEnabled(false);
        }
        this.setLocation(300, 300);
        this.setLocationRelativeTo(parent);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        saveEnd = new javax.swing.JCheckBox();
        blurring = new javax.swing.JCheckBox();
        saveStep = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        monoton = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        saveEnd.setText("Save the result after processing automatically");
        saveEnd.setToolTipText("Opens the dialog after processing");
        saveEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveEndActionPerformed(evt);
            }
        });

        blurring.setText("Blur the image before processing");
        blurring.setToolTipText("Preprocesses the image for avoid errors caused by inaccurate image registration");

        saveStep.setText("Save the result after every step");
        saveStep.setToolTipText("Opens the save dialog after every two joined images");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lookup Table Smoothing"));

        buttonGroup2.add(jRadioButton3);
        jRadioButton3.setText("No smoothing");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        buttonGroup2.add(jRadioButton4);
        jRadioButton4.setText("Moving average smoothing");

        buttonGroup2.add(jRadioButton5);
        jRadioButton5.setText("Median smoothing");
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        monoton.setText("Make curve non-decreasing");
        monoton.setToolTipText("\"Monotonization\" of the curve");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton4)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton5)
                    .addComponent(monoton))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton5)
                .addGap(18, 18, 18)
                .addComponent(monoton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Used Method"));

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Histogram Statistics Method");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Direct Level Mapping");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton1))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(199, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(blurring)
                    .addComponent(saveEnd)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(saveStep))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(blurring)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveEnd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveStep)
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    ButtonModel bm = buttonGroup1.getSelection();
    int met = 0;
    if (bm == jRadioButton1.getModel()) {
        met = Constants.METHOD_LUT;
    } else if (bm == jRadioButton2.getModel()) {
        met = Constants.METHOD_STRETCHING;
    }
    prefs.putInt(Constants.METHOD_CHOICE, met);

//            prefs.putInt(Constants.CS_TYPE, csCombo.getSelectedIndex());
//            prefs.putInt(Constants.COMP_PROC, Utils.getCompProc(new boolean[]{
//                        compCheck[0].isSelected(),
//                        compCheck[1].isSelected(),
//                        compCheck[2].isSelected()
//                    }));
//            prefs.putInt(Constants.METHOD_CHOICE, methodCombo.getSelectedIndex());
//            prefs.putInt(Constants.LUT_ACCURACY, (Integer) lutAccSpin.getValue());

    ButtonModel bm2 = buttonGroup2.getSelection();
    int smooth = 0;
    if (bm2 == jRadioButton3.getModel()) {
        smooth = 0;
    } else if (bm2 == jRadioButton4.getModel()) {
        smooth = 1;
    } else if (bm2 == jRadioButton5.getModel()) {
        smooth = 2;
    }
    prefs.putInt(Constants.SMOOTHING, smooth);
    prefs.putBoolean(Constants.MONOTON, monoton.isSelected());
    
    prefs.putInt(Constants.BLUR_RADIUS, blurring.isSelected() ? 10 : 0);
    int save = 0;
    if (saveEnd.isSelected()) {
        save++;
        if (saveStep.isSelected()) {
            save++;
        }
    }
    prefs.putInt(Constants.SAVE_END, save);

    try {
        prefs.flush();
    } catch (BackingStoreException ex) {
        Logger.getLogger(OptionsDialog.class.getName()).log(Level.SEVERE, null, ex);
    }
    setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

private void saveEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveEndActionPerformed
    if (saveStep.isEnabled()) {
        saveStep.setEnabled(false);
    } else {
        saveStep.setEnabled(true);
    }
}//GEN-LAST:event_saveEndActionPerformed

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
    ButtonModel bm = buttonGroup1.getSelection();
    if (bm == jRadioButton1.getModel()) {
        jRadioButton3.setEnabled(true);
        jRadioButton4.setEnabled(true);
        jRadioButton5.setEnabled(true);
        monoton.setEnabled(true);
    } else if (bm == jRadioButton2.getModel()) {
        jRadioButton3.setEnabled(false);
        jRadioButton4.setEnabled(false);
        jRadioButton5.setEnabled(false);
        monoton.setEnabled(false);
    }
}//GEN-LAST:event_jRadioButton2ActionPerformed

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
    ButtonModel bm = buttonGroup1.getSelection();
    if (bm == jRadioButton1.getModel()) {
        jRadioButton3.setEnabled(true);
        jRadioButton4.setEnabled(true);
        jRadioButton5.setEnabled(true);
        monoton.setEnabled(true);
    } else if (bm == jRadioButton2.getModel()) {
        jRadioButton3.setEnabled(false);
        jRadioButton4.setEnabled(false);
        jRadioButton5.setEnabled(false);
        monoton.setEnabled(false);
    }
}//GEN-LAST:event_jRadioButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    super.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jRadioButton3ActionPerformed

private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jRadioButton5ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                OptionsDialog2 dialog = new OptionsDialog2(new javax.swing.JFrame(), "Preferences", true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox blurring;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JCheckBox monoton;
    private javax.swing.JCheckBox saveEnd;
    private javax.swing.JCheckBox saveStep;
    // End of variables declaration//GEN-END:variables

}
